package com.example.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName FlowLimitController
 * @Author Admin
 * @Date 2020/9/16 15:56
 * @Version 1.0
 **/
@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA() {
        return "---------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "---------testB";
    }

    @GetMapping("/testC")
    public String testC() {
        /*try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("------------------测试RT");*/
        log.info("测试异比列");
        int age = 10 / 0;
        return "---------testC测试异比例";
    }

    @GetMapping("/testD")
    public String testD() {
        log.info("测试异常数");
        int age = 10 / 0;
        return "---------testD测试异常数";
    }

    /**
     * 热点限流测试
     * SentinelResource 里面的value 可以随便写，只是为了统一，所以才写的和请求路径的url一样
     *
     * @return
     */
    @GetMapping(value = "/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1, @RequestParam(value = "p2", required = false) String p2) {
        return "---------testE热点Key 测试使用";
    }

    public String deal_testHotKey(String p1, String p2, BlockException exception) {
        return "---------超过每秒钟请求限定的QPS,deal_testHotKey o(╥﹏╥)o ！";
    }

}
